package com.supermap.dataHandling.service;

import com.supermap.dataHandling.utils.ExcelUtil;
import com.supermap.dataHandling.utils.FileTool;
import com.supermap.dataHandling.utils.StringUtil;
import com.supermap.dataHandling.utils.WordUtil;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WordService {

    private String filePath;
    private String fileName;
    private String fileOnlyName;

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileOnlyName() {
        return fileOnlyName;
    }

    public void setFileOnlyName(String fileOnlyName) {
        this.fileOnlyName = fileOnlyName;
    }

    /**
     * 匹配word模板数据
     * @param directoryDir
     * @param year
     * @param month
     * @param month_1
     * @return
     */
    public File createWord(String directoryDir, String year, String month, String month_1){
        Map<String, Object> dataMap = new HashMap<>();
        Map<Object,String> rootListFileName = FileTool.getRootListFileName(directoryDir);


        //读取房地产整百万修改数据放入模板
        List<List<String>> zhengfd = ExcelUtil.readExcels(rootListFileName.get("房地产整百万修改.xls"));
        List<List<String>> zhengfdList = ExcelUtil.formatExcel(zhengfd);
        List<Map<String,Object>> zhengfdInfo = new ArrayList<>();
        for (int a = 0; a<zhengfd.size();a++){
            Map<String,Object> map = new HashMap<>();
            map.put("code",zhengfdList.get(0).get(a));
            map.put("name",zhengfdList.get(1).get(a));
            map.put("before",zhengfdList.get(2).get(a));
            map.put("after",zhengfdList.get(3).get(a));
            map.put("cha",zhengfdList.get(4).get(a));
            String dataName = StringUtil.parseDataName(zhengfdList.get(5).get(a));
            map.put("dataname",dataName);
            map.put("time",zhengfdList.get(6).get(a));
            zhengfdInfo.add(map);
        }

        //读取服务业整百万修改数据放入模板
        List<List<String>> zhengfw = ExcelUtil.readExcels(rootListFileName.get("服务业整百万修改.xls"));
        List<List<String>> zhengfwList = ExcelUtil.formatExcel(zhengfw);
        List<Map<String,Object>> zhengfwInfo = new ArrayList<>();
        for (int b = 0; b<zhengfw.size();b++){
            Map<String,Object> map = new HashMap<>();
            map.put("code",zhengfwList.get(0).get(b));
            map.put("name",zhengfwList.get(1).get(b));
            map.put("before",zhengfwList.get(2).get(b));
            map.put("after",zhengfwList.get(3).get(b));
            map.put("cha",zhengfwList.get(4).get(b));
            String dataName = StringUtil.parseDataName(zhengfwList.get(5).get(b));
            map.put("dataname",dataName);
            map.put("time",zhengfwList.get(6).get(b));
            zhengfwInfo.add(map);
        }

        //读取工业整百万修改数据放入模板
        List<List<String>> zhengb = ExcelUtil.readExcels(rootListFileName.get("工业整百万修改.xls"));
        List<List<String>> zhengbList = ExcelUtil.formatExcel(zhengb);
        //System.out.println(zhengbList);
        List<Map<String,Object>> zhengbInfo = new ArrayList<>();
        for (int c = 0; c<zhengb.size();c++){
            Map<String,Object> map = new HashMap<>();
            map.put("code",zhengbList.get(0).get(c));
            map.put("name",zhengbList.get(1).get(c));
            map.put("before",zhengbList.get(2).get(c));
            map.put("after",zhengbList.get(3).get(c));
            map.put("cha",zhengbList.get(4).get(c));
            String dataName = StringUtil.parseDataName(zhengbList.get(5).get(c));
            map.put("dataname",dataName);
            map.put("time",zhengbList.get(6).get(c));
            zhengbInfo.add(map);
        }

        //读取建筑业整百万修改数据放入模板
        List<Map<String,Object>> zhengjInfo = new ArrayList<>();
        List<List<String>> zhengj = ExcelUtil.readExcels(rootListFileName.get("建筑业整百万修改.xls"));
        if (zhengj == null){
            //System.out.println("meiyouzhi");
            zhengjInfo.add(null);
        }else {
            List<List<String>> zhengjList = ExcelUtil.formatExcel(zhengj);
            for (int c = 0; c<zhengj.size();c++){
                Map<String,Object> map = new HashMap<>();
                map.put("code",zhengjList.get(0).get(c));
                map.put("name",zhengjList.get(1).get(c));
                //System.out.println(zhengjList.get(1).get(c));
                map.put("before",zhengjList.get(2).get(c));
                map.put("after",zhengjList.get(3).get(c));
                map.put("cha",zhengjList.get(4).get(c));
                String dataName = StringUtil.parseDataName(zhengjList.get(5).get(c));
                if (dataName != null){
                    map.put("dataname",dataName);
                }
                map.put("time",zhengjList.get(6).get(c));
                zhengjInfo.add(map);
            }
        }



        //读取贸经整百万修改数据放入模板
        List<List<String>> zhengm = ExcelUtil.readExcels(rootListFileName.get("贸经整百万修改.xls"));
        List<List<String>> zhengmList = ExcelUtil.formatExcel(zhengm);
        List<Map<String,Object>> zhengmInfo = new ArrayList<>();
        for (int c = 0; c<zhengm.size();c++){
            Map<String,Object> map = new HashMap<>();
            map.put("code",zhengmList.get(0).get(c));
            map.put("name",zhengmList.get(1).get(c));
            map.put("before",zhengmList.get(2).get(c));
            map.put("after",zhengmList.get(3).get(c));
            map.put("cha",zhengmList.get(4).get(c));
            String dataName = StringUtil.parseDataName(zhengmList.get(5).get(c));
            map.put("dataname",dataName);
            map.put("time",zhengmList.get(6).get(c));
            zhengmInfo.add(map);
        }

        //读取内网代报数据放入模板
        List<List<String>> neiwang = ExcelUtil.readExcels(rootListFileName.get("内网代报企业.xls"));
        List<List<String>> neiwangList = ExcelUtil.formatExcel(neiwang);
        List<Map<String,Object>> neiwangInfo = new ArrayList<>();
        for (int c = 0; c<neiwang.size();c++){
            Map<String,Object> map = new HashMap<>();
            map.put("code",neiwangList.get(0).get(c));
            map.put("name",neiwangList.get(1).get(c));
            String s = neiwangList.get(2).get(c);
            if ("B".equals(s)){
                map.put("major","工业");
            }else if ("FD".equals(s)){
                map.put("major","房地产");
            }else if("FW".equals(s)){
                map.put("major","服务业");
            }else if ("J".equals(s)){
                map.put("major","建筑业");
            }else if ("M".equals(s)){
                map.put("major","贸经");
            }
            //map.put("major",neiwangList.get(2).get(c));
            map.put("ip",neiwangList.get(3).get(c));
            map.put("dataplace",neiwangList.get(4).get(c));
            neiwangInfo.add(map);
        }

        //读取能耗异常
        List<List<String>> nenghao = ExcelUtil.readExcels(rootListFileName.get("能耗异常.xls"));
        List<List<String>> nenghaoList = ExcelUtil.formatExcel(nenghao);
        List<Map<String,Object>> nenghaoInfo = new ArrayList<>();
        for (int i = 0;i<nenghao.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("code",nenghaoList.get(0).get(i));
            map.put("area",nenghaoList.get(1).get(i));
            map.put("name",nenghaoList.get(2).get(i));
            map.put("total",nenghaoList.get(3).get(i));
            map.put("mei",nenghaoList.get(4).get(i));
            nenghaoInfo.add(map);
        }

        //读取修改率数据放入模板
        List<List<String>> xiugai = ExcelUtil.readExcels(rootListFileName.get("修改率表格.xls"));
        List<List<String>> xiugaiList = ExcelUtil.formatExcel(xiugai);
        List<Map<String,Object>> xiugaiInfo = new ArrayList<>();
        for (int c = 0; c<xiugai.size();c++){
            Map<String,Object> map = new HashMap<>();
            map.put("area",xiugaiList.get(0).get(c));
            map.put("b",xiugaiList.get(1).get(c));
            map.put("fd",xiugaiList.get(2).get(c));
            map.put("fw",xiugaiList.get(3).get(c));
            map.put("j",xiugaiList.get(4).get(c));
            map.put("m",xiugaiList.get(5).get(c));
            map.put("z",xiugaiList.get(6).get(c));
            xiugaiInfo.add(map);
        }

        //读取一址多报数据放入模板
        List<List<String>> yizhi = ExcelUtil.readExcels(rootListFileName.get("一址多报表格.xls"));
        List<List<String>> yizhiList = ExcelUtil.formatExcel(yizhi);
        List<Map<String,Object>> yizhiInfo = new ArrayList<>();
        for (int c = 0; c<yizhi.size();c++){
            Map<String,Object> map = new HashMap<>();
            map.put("area",yizhiList.get(0).get(c));
            map.put("b",yizhiList.get(1).get(c));
            map.put("fd",yizhiList.get(2).get(c));
            map.put("fw",yizhiList.get(3).get(c));
            map.put("m",yizhiList.get(4).get(c));
            map.put("j",yizhiList.get(5).get(c));
            map.put("total",yizhiList.get(6).get(c));
            yizhiInfo.add(map);
        }

        //读取直报率数据放入模板
        List<List<String>> zhiabo = ExcelUtil.readExcels(rootListFileName.get("直报率表格.xls"));
        List<List<String>> zhiaboList = ExcelUtil.formatExcel(zhiabo);
        List<Map<String,Object>> zhiaboInfo = new ArrayList<>();
        for (int c = 0; c<zhiabo.size();c++){
            Map<String,Object> map = new HashMap<>();
            map.put("area",zhiaboList.get(0).get(c));
            map.put("b",zhiaboList.get(1).get(c));
            map.put("m",zhiaboList.get(2).get(c));
            map.put("fd",zhiaboList.get(3).get(c));
            map.put("fw",zhiaboList.get(4).get(c));
            map.put("j",zhiaboList.get(5).get(c));
            map.put("z",zhiaboList.get(6).get(c));
            zhiaboInfo.add(map);
        }




        String season = null;
        if ("3".equals(month)){
            season = "1";
        }else if ("6".equals(month)){
            season = "2";
        }else if ("9".equals(month)){
            season = "3";
        }else if ("12".equals(month)){
            season = "4";
        }
        dataMap.put("zhibao",zhiaboInfo);
        dataMap.put("xiugai",xiugaiInfo);
        dataMap.put("zhengfd",zhengfdInfo);
        dataMap.put("zhengb",zhengbInfo);
        dataMap.put("zhengm",zhengmInfo);
        dataMap.put("zhengfw",zhengfwInfo);
        dataMap.put("zhengj",zhengjInfo);
        dataMap.put("yizhi",yizhiInfo);
        dataMap.put("neiwang",neiwangInfo);
        dataMap.put("nenghao",nenghaoInfo);
        dataMap.put("year",year);
        dataMap.put("month",month);
        dataMap.put("month_1",month_1);
        dataMap.put("season",season);

        //文件路径
        filePath = "F:\\doc_f\\";
        //文件唯一名称
        fileOnlyName = month + "月联网直报模板";
        //文件名称
        fileName = year + "年" + month + "月联网直报模板.doc";

        if (month.equals("3") || month.equals("6") || month.equals("9") || month.equals("12")){
            //生成word
            WordUtil.createWord(dataMap, "lianwang_hanjianzhu.ftl", filePath, fileName);
        }else {
            WordUtil.createWord(dataMap, "lianwang_buhanj.ftl", filePath, fileName);
        }
        File file = new File(filePath + File.separator + fileName);
        return file;
    }

    /**
     * 下载word文档
     *
     * @return
     */
    public boolean downloadWord() {
        boolean flag = false;
        try {
            filePath = URLDecoder.decode(filePath, "UTF-8");
            fileName = URLDecoder.decode(fileName, "UTF-8");
            fileOnlyName = URLDecoder.decode(fileOnlyName, "UTF-8");

            new FileInputStream(filePath + File.separator + fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return flag;
        }
        flag = true;
        return flag;
    }

    public InputStream getWordFile() {
        try {
            fileName = URLDecoder.decode(fileName, "UTF-8");

            return new FileInputStream(filePath + File.separator + fileOnlyName);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
